<!-- 资质信息 -->
<template>
  <div class="app-container">
    <div v-if="buyShow">
      <div class="header">
        <div class="bread">
          <i class="active iconfont icon-xingzhuangjiehe"></i>
          <span style="margin-left: 10px">资质维护</span>
        </div>
        <div class="ipt">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="请输入资质名称"
            size="normal"
            clearable
            class="ipt-el"
          ></el-input>
          <el-button type="primary" size="default" @click="handleQuery">
            搜索
          </el-button>
        </div>
        <div class="btn">
          <div>
            <el-button
              size="default"
              @click="handleAdd"
              v-hasPermi="['qualification:qualificationInfo:add']"
              style="margin-right: 10px; background-color: #ff6a00; color: #fff"
            >
              <span>+</span> 新建资质
            </el-button>
            <el-dropdown>
              <el-button class="el-icon-more"></el-button>
              <el-dropdown-menu slot="dropdown"> </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </div>
      <div class="chooise">
        <template>
          <span>场景：</span>
          <el-select
            v-model="value"
            ref="sel"
            clearable
            @visible-change="
              (v) => visibleChange(v, 'sel', createScene, configScene)
            "
            class="sel-sence"
            @change="querySceneList"
            style="margin-right: 10px"
          >
            <el-option
              v-for="item in sceneOptions"
              :key="item.sceneId"
              :label="item.name"
              :value="item.sceneId"
            >
            </el-option>
          </el-select>
          <el-button
            type="primary"
            icon="iconfont icon-shaixuan"
            size="default"
            @click="createScene"
          >
            高级筛选
          </el-button>
        </template>
      </div>
      <div v-if="scenArr !== []" class="scenF">
        <div
          v-for="(item, index) in scenArr"
          :key="index"
          class="scenF-content"
        >
          <div class="scenF-detail" @click="scenSearch(item.sceneId)">
            {{ item.sceneList }}
            <i
              class="el-icon-circle-close"
              @click.stop="scenDel(index)"
              style="margin-left: 10px"
            ></i>
          </div>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="qualificationInfoList"
        @selection-change="handleSelectionChange"
      >
        <template slot="empty" class="emptyBg">
          <img src="@/assets/images/empty.jpg" alt="" />
        </template>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="资质名称"
          align="center"
          prop="qualificationTypeId"
        >
          <template slot-scope="scope">
            {{ scope.row.qualificationType.name }}
          </template>
        </el-table-column>
        <el-table-column label="公司名称" align="center" prop="companyName" />
        <el-table-column label="资质编号" align="center" prop="certNumber" />
        <el-table-column
          label="有效期"
          align="center"
          prop="validityDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.foreverValidity == "Y"
                ? "永久有效"
                : parseTime(scope.row.validityDate, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="发证时间"
          align="center"
          prop="issueDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.issueDate, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="发证机关" align="center" prop="issueUnit" />

        <el-table-column label="状态" align="center" prop="certNumber">
          <template slot-scope="scope">
            {{
              scope.row.foreverValidity == "Y"
                ? "未过期"
                : compareDate(scope.row.validityDate)
                ? "未过期"
                : "已过期"
            }}
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="负责人" align="center" prop="directorName" />
        <el-table-column
          label="更新时间"
          align="center"
          prop="updateTime"
          width="180"
        />
        <el-table-column label="创建人" align="center" prop="createBy" />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改资质信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="53%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="资质名称" prop="qualificationTypeId">
                <treeselect
                  :disable-branch-nodes="true"
                  v-model="form.qualificationTypeId"
                  :options="qualificationTypeTreeData"
                  :show-count="true"
                  placeholder="请选择资质名称"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="公司名称" prop="companyName">
                <el-input
                  v-model="form.companyName"
                  placeholder="请输入公司名称"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="发证机关" prop="issueUnit">
                <el-input
                  v-model="form.issueUnit"
                  placeholder="请输入发证机关"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发证时间" prop="issueDate">
                <el-date-picker
                  clearable
                  size="small"
                  style="width: 100%"
                  v-model="form.issueDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择发证时间"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="证书编号" prop="certNumber">
                <el-input
                  v-model="form.certNumber"
                  placeholder="请输入证书编号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="有效期"
                prop="validityDate"
                v-if="!form.foreverValidity"
              >
                <el-date-picker
                  clearable
                  size="small"
                  style="width: 100%"
                  v-model="form.validityDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择有效期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="永久有效" prop="foreverValidity">
                <el-checkbox v-model="form.foreverValidity"></el-checkbox>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="备注信息" prop="remark">
                <el-input
                  v-model="form.remark"
                  type="textarea"
                  :rows="3"
                  placeholder="请填写备注信息"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <manage-drawer
        :dialog="dialog"
        :drawerData="form"
        v-if="dialog.dialogShowDrawerEject"
      ></manage-drawer>

      <manage-screen
        :dialog="dialog"
        v-if="dialog.screenEcejt"
        @sceneArr="sceneArr"
      ></manage-screen>
      <drag-table
        :dialog="dialog"
        v-if="dialog.senceAdminEject"
        ref="dragTable"
      ></drag-table>
    </div>
    <div v-else style="margin-top: 20px">
      <purchase :type="type"></purchase>
    </div>
  </div>
</template>

<script>
import {
  getQualificationInfo,
  delQualificationInfo,
  addQualificationInfo,
  updateQualificationInfo,
  exportQualificationInfo,
} from "@/api/qualification/qualificationInfo";
import { check } from "@/api/purchase";
import ManageScreen from "./components/manageScreen.vue";
import manageDrawer from "./components/manageDrawer.vue";
import DragTable from "./components/dragTable.vue";
import { unTrusteeshipList } from "@/api/enQualification/qualificationDeposit";
import { qualTreeselect } from "@/api/qualification/qualificationType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { queryScene } from "@/api/currency";
import Purchase from "@/components/purchase";
export default {
  name: "QualificationInfo",
  components: {
    manageDrawer,
    Treeselect,
    ManageScreen,
    DragTable,
    Purchase,
  },
  data() {
    return {
      Str: "",
      scenArr: [],
      value: "",
      type: 2,
      buyShow: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资质信息表格数据
      qualificationInfoList: [],
      qualificationTypeTreeData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qualificationTypeId: null,
        companyName: null,
        issueUnit: null,
        issueDate: null,
        certNumber: null,
        validityDate: null,
        foreverValidity: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qualificationTypeId: [
          { required: true, message: "请选择资质名称", trigger: "blur" },
        ],
        certNumber: [
          { required: true, message: "请输入证书编号", trigger: "blur" },
        ],
        validityDate: [
          { required: true, message: "选择有效期", trigger: "blur" },
        ],
      },
      //场景
      sceneOptions: [],
      dialog: {
        dialogShowDrawer: false,
        dialogShowDrawerEject: false,
        dialogShowChioos: false,
        dialogShowBilling: false,
        dialogScreenVisible: false,
        senceAdmin: false, //场景管理
        senceAdminEject: false,
        screenEcejt: false,
        printShow: false,
        printShowEject: false,
      },
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getScene();
  },
  methods: {
    //查看
    drawer(row) {
      this.dialog.dialogShowDrawerEject = true;
      this.$nextTick(function () {
        this.dialog.dialogShowDrawer = true;
        this.form = row;
      });
    },
    // 删除临时搜索
    scenDel(i) {
      this.scenArr.splice(i, 1);
      if (this.scenArr.length == 0) {
        this.getList();
      }
    },
    // 获取数组
    sceneArr(data) {
      this.scenArr.push(data);
    },
    querySceneList() {
      this.getList(this.value);
    },
    // 请求场景
    getScene() {
      queryScene({
        type: 29,
      }).then((res) => {
        if (res.default !== null) {
          res.scenes.unshift(res.default);
          this.sceneOptions = res.scenes;
        } else {
          this.sceneOptions = res.scenes;
        }
      });
    },
    // 场景下拉框按钮
    visibleChange(visible, refName, onClick, onClick2) {
      if (visible) {
        const ref = this.$refs[refName];
        let popper = ref.$refs.popper;
        if (popper.$el) popper = popper.$el;
        if (
          !Array.from(popper.children).some(
            (v) => v.className === "el-cascader-menu__list"
          )
        ) {
          const el = document.createElement("ul");
          el.className = "el-cascader-menu__list";
          el.style =
            "border-top: solid 1px #E4E7ED; padding:0; color: #606266;";
          el.innerHTML = `<li class="el-cascader-node" >
                            <span class="el-cascader-node__label" style="color:#1267FA">新建场景</span>
                            </li>
                        `;
          const el2 = document.createElement("ul");
          el2.className = "el-cascader-menu__list";
          el2.innerHTML = `<li class="el-cascader-node" >
                            <span class="el-cascader-node__label" style="color:#1267FA">管理</span>
                            </li>
                        `;
          popper.appendChild(el);
          popper.appendChild(el2);
          el2.onclick = () => {
            onClick2 && onClick2();
            if (ref.toggleDropDownVisible) {
              ref.toggleDropDownVisible(false);
            } else {
              ref.visible = false;
            }
          };
          el.onclick = () => {
            // 底部按钮的点击事件 点击后想触发的逻辑也可以直接写在这
            onClick && onClick();
            // 以下代码实现点击后弹层隐藏 不需要可以删掉
            if (ref.toggleDropDownVisible) {
              ref.toggleDropDownVisible(false);
            } else {
              ref.visible = false;
            }
          };
        }
      }
    },
    // 点击临时搜索
    scenSearch(id) {
      this.getList(id);
    },
    //管理
    configScene() {
      this.dialog.senceAdminEject = true;
      this.$nextTick(function () {
        this.dialog.senceAdmin = true;
      });
    },
    //新建场景
    createScene() {
      this.dialog.screenEcejt = true;
      this.$nextTick(function () {
        this.dialog.dialogScreenVisible = true;
      });
    },
    getTreeselect() {
      qualTreeselect().then((response) => {
        this.qualificationTypeTreeData = response.data;
        this.createAllowedTree(this.qualificationTypeTreeData);
      });
    },
    createAllowedTree(treeData) {
      treeData.forEach((item) => {
        if (item.children && item.children.length > 0) {
          this.createAllowedTree(item.children);
        } else {
          if (item.type == "0") {
            item.children = [];
          }
          item.isDisabled = item.type == "0" ? true : false;
        }
      });
    },
    /** 查询资质信息列表 */
    getList(val) {
      this.loading = true;
      unTrusteeshipList({
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        sceneId: val,
        searchValue: this.queryParams.searchValue,
      }).then((response) => {
        this.qualificationInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        qualificationTypeId: null,
        companyName: null,
        issueUnit: null,
        issueDate: null,
        certNumber: null,
        validityDate: null,
        foreverValidity: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.Str = "";
      selection.forEach((item) => {
        console.log(item);
        this.Str += item.qualificationType.name + ",";
      });
      console.log(this.Str);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资质信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getQualificationInfo(id).then((response) => {
        this.form = response.data;
        this.form.foreverValidity =
          response.data.foreverValidity == "Y" ? true : false;
        this.open = true;
        this.title = "修改资质信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let _form = JSON.parse(JSON.stringify(this.form));
          _form.foreverValidity = _form.foreverValidity ? "Y" : "N";
          if (_form.id != null) {
            updateQualificationInfo(_form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addQualificationInfo(_form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除资质信息?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delQualificationInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportQualificationInfo(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>
<style lang="less" scoped>
.header {
  width: 100%;
  height: 74px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #ececec;

  .bread {
    margin-top: 20px;
    margin-left: 5px;
    height: 30px;
    display: flex;
    vertical-align: middle;
    line-height: 30px;

    i {
      font-size: 30px;
      color: #bdbdbd;
    }

    p {
      margin-left: 10px;
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #2b2b2b;
    }
  }

  .ipt {
    margin: auto;
    margin-top: 16px;
    width: 350px;
    transform: translateX(-20%);

    /deep/ .el-input__inner {
      border-radius: 4px 0 0 4px;
    }

    .el-button--primary {
      border-radius: 0 4px 4px 0;
    }
  }
}

.chooise {
  margin-top: 20px;

  .sel {
    margin: 0 14px;
  }

  .el-button {
    font-size: 14px;

    /deep/ span {
      margin-left: 5px;
    }
  }
}

.active {
  color: #1172fb !important;
}

.active1 {
  color: #ff8e43 !important;
}

.sel {
  margin-top: 20px;

  span {
    margin-right: 40px;
  }
}

.footer {
  height: 30px;

  .el-pagination {
    margin-top: 10px;
  }

  /deep/ .btn-prev {
    width: 80px;
    height: 40px;
    background: #fafafa;
    border-radius: 4px;
    border: 1px solid #d3dbeb;
  }

  /deep/ .btn-next {
    width: 80px;
    height: 40px;
    background: #fafafa;
    border-radius: 4px;
    border: 1px solid #d3dbeb;
  }

  /deep/ .number {
    width: 40px;
    height: 40px;
    line-height: 40px;
    background: #ffffff;
    border-radius: 4px;
    border: 1px solid #8f9bb3;
  }

  /deep/ .btn-quicknext {
    width: 40px;
    height: 40px;
    line-height: 40px;
    background: #ffffff;
    border-radius: 4px;
    border: 1px solid #8f9bb3;
  }

  /deep/ .btn-quickprev {
    width: 40px;
    height: 40px;
    line-height: 40px;
    background: #ffffff;
    border-radius: 4px;
    border: 1px solid #8f9bb3;
  }

  /deep/ .el-pagination__total {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #2b2b2b;
    line-height: 24px;
    margin-top: 8px;
  }

  /deep/ .el-pagination__jump {
    margin-top: 6px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #2b2b2b;
    line-height: 14px;
  }
}

.batch {
  color: #1267fa;
}

.btn {
  display: flex;
  margin-top: 15px;
}

.basics {
  margin-left: 20px;
  position: relative;
  font-size: 14px;
}

.basics::before {
  content: "";
  position: absolute;
  left: -6px;
  top: 1px;
  width: 3px;
  height: 14px;
  background: #1267fa;
}

.basics-infomation {
  display: flex;
  flex-wrap: wrap;
}

.el-form-item {
  margin-left: 20px;
}

.table {
  position: relative;

  .wuhu {
    position: absolute;
    top: 45px;
    right: 0px;
    z-index: 9999;
    background-color: #fff;
    border: 1px solid #ececec;
  }
}

.importInfo {
  /deep/ .el-dialog__body {
    display: flex;
    justify-content: space-around;

    .module {
      width: 40%;
      height: 300px;

      .module-img {
        width: 49px;
        height: 60px;
        margin: 50%;
        transform: translate(-50%, -50%);

        img {
          width: 100%;
          height: 100%;
        }

        .el-button {
          margin-top: 10px;
          transform: translateX(-25%);
        }
      }

      /deep/ .el-dialog__footer {
        text-align: left;
      }

      .module-txt {
        transform: translateY(400%);
        text-align: center;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #1f2e4d;
      }
    }
  }

  .dashed {
    width: 1px;
    height: 180px;
    border-left: 2px dashed rgba(0, 0, 0, 0.1);
    transform: translateY(35%);
  }

  /deep/ .el-upload-list {
    display: none;
  }
}

.ipt-el {
  width: 240px;
}

.scenF {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;

  .scenF-content {
    display: flex;
    margin-left: 10px;
    margin-top: 10px;

    .scenF-detail {
      height: 36px;
      line-height: 36px;
      padding-left: 20px;
      padding-right: 20px;
      border: 1px solid #9aa1af;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;

      .el-icon-circle-close {
        transform: translateY(3px);
      }
    }
  }
}
</style>




